package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.wemedia.dtos.WmAuthDto;
import com.heima.wemedia.dtos.WmNewsDto;
import com.heima.wemedia.dtos.WmNewsPageReqDto;
import com.heima.wemedia.pojos.WmNews;
import com.heima.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmUserService;
import com.heima.wemedia.vos.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(("/api/v1/news"))
public class NewsController {

    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private WmUserService wmUserService;

    @GetMapping("/wmUser")
    public ResponseResult<WmUser> user(@RequestParam Long userId) {
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getId, userId);
        WmUser user = wmUserService.getOne(wrapper);
        return ResponseResult.okResult(user);
    }

    /**
     * 文章列表
     *
     * @param wmNewsPageReqDto
     * @return
     */
    @PostMapping("/list")
    public PageResponseResult list(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        Page<WmNews> page = wmNewsService.listPage(wmNewsPageReqDto);
        return PageResponseResult.getPageResponseResult(page);
    }

    /**
     * 文章发布(自动审核)
     *
     * @param wmNewsDto 前端dto 自媒体发布文章的dto数据
     * @return 返回结果
     */
    @PostMapping("/submit")
    public ResponseResult submit(@RequestBody WmNewsDto wmNewsDto) {
        wmNewsService.submit(wmNewsDto);
        return ResponseResult.okResult(null);
    }

    /**
     * 文章上下架
     *
     * @param wmNewsDto
     * @return
     */
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto wmNewsDto) {
        wmNewsService.downOrUp(wmNewsDto);
        return ResponseResult.okResult(null);
    }

    /**
     * 查询自媒体文章详情
     *
     * @param id 自媒体文章id
     * @return
     */
    @GetMapping("/one/{id}")
    public ResponseResult getWmMaterial(@PathVariable Integer id) {
        ResponseResult rs;
        WmNews wmNews = wmNewsService.getById(id);
        if (wmNews != null) {
            rs = ResponseResult.okResult(wmNews);
        } else {
            rs = ResponseResult.errorResult(AppHttpCodeEnum.ARTICLE_NOT_EXIST);
        }
        return rs;
    }

    /**
     * 文章删除
     *
     * @param id 文章id
     */
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable Long id) {
        // wmNewsService.delNews(id);
        return wmNewsService.delNews(id);
    }

    /**
     * 人工审核分页查询
     * @param pageRequestDto 前端dto <br/>page页码 size每页显示条数<br/> status状态 title标题 name名称
     * @return 当前页显示内容
     */
    @PostMapping("/list_vo")
    public PageResponseResult list(@RequestBody PageRequestDto pageRequestDto){
       Page<WmNewsVo> page = wmNewsService.listVO(pageRequestDto);
       return PageResponseResult.getPageResponseResult(page);
    }

    /**
     * 查询文章详情
     * @param id 文章id
     * @return 内容
     */
    @GetMapping("/one_vo/{id}")
    public ResponseResult one(@PathVariable Long id) {
        WmNewsVo wmNewsVo = wmNewsService.oneVO(id);
        return ResponseResult.okResult(wmNewsVo);
    }

    /**
     * 人工审核失败(驳回)
     * @param wmAuthDto 文章id 驳回原因
     */
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody WmAuthDto wmAuthDto){
        wmNewsService.authFail(wmAuthDto);
        return ResponseResult.okResult(null);
    }

    /**
     * 人工审核通过
     * @param wmAuthDto 通过审核的文章id
     * @return 结果
     */
    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody WmAuthDto wmAuthDto){
        wmNewsService.authPass(wmAuthDto);
        return ResponseResult.okResult(null);
    }
}
